package com.chaihu.db.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.chaihu.core.factory.AuthenticationFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

//实现自动填充处理器
@Component //需要注册到Spring容器中
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 插入时自动填充
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("createBy", getUserInfo(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 更新时自动填充
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", getUserInfo(), metaObject);
    }

    private String getUserInfo() {
        return "登录的人";// TODO 从session中获取用户信息  MDC.get()?
    }

}
